<div class="m-bg">
<h1>SQL BETWEEN 操作符</h1>
<p class="intro">BETWEEN 操作符用于选取介于两个值之间的数据范围内的值。</p> <h2>SQL BETWEEN 操作符</h2> <p>BETWEEN 操作符选取介于两个值之间的数据范围内的值。这些值可以是数值、文本或者日期。</p> <h3>SQL BETWEEN 语法</h3> <div class="code notranslate"><pre><code class="language-sql"><div> SELECT <em>column_name(s)</em><br/> FROM <em>table_name</em><br/> WHERE <em>column_name </em>BETWEEN <em>value1</em> AND <em>value2;</em> </div></code></pre></div> <h2>演示数据库</h2> <p>在本教程中，我们将使用众所周知的 Northwind 样本数据库。</p> <p>下面是选自 "Products" 表的数据：</p> <table class="reference notranslate">
<tr>
<th>ProductID</th> <th>ProductName</th> <th>SupplierID</th> <th>CategoryID</th> <th>Unit</th> <th>Price</th> </tr>
<tr>
<td>1</td> <td>Chais</td> <td>1</td> <td>1</td> <td>10 boxes x 20 bags</td> <td>18</td> </tr>
<tr>
<td>2</td> <td>Chang</td> <td>1</td> <td>1</td> <td>24 - 12 oz bottles</td> <td>19</td> </tr>
<tr>
<td>3</td> <td>Aniseed Syrup</td> <td>1</td> <td>2</td> <td>12 - 550 ml bottles</td> <td>10</td> </tr>
<tr>
<td>4</td> <td>Chef Anton's Cajun Seasoning</td> <td>1</td> <td>2</td> <td>48 - 6 oz jars</td> <td>22</td> </tr>
<tr>
<td>5</td> <td>Chef Anton's Gumbo Mix</td> <td>1</td> <td>2</td> <td>36 boxes</td> <td>21.35</td> </tr>
</table>
<h2>BETWEEN 操作符实例</h2> <p>下面的 SQL 语句选取价格介于 10 和 20 之间的所有产品：</p> <div class="example margin-b-10"> <h2 class="example">实例</h2> <pre><code class="language-sql"><div class="example_code notranslate"> SELECT * FROM Products<br/>WHERE Price BETWEEN 10 AND 20; </div></code></pre> </div> <h2>NOT BETWEEN 操作符实例</h2> <p>如需显示不在上面实例范围内的产品，请使用 NOT BETWEEN：</p> <div class="example margin-b-10"> <h2 class="example">实例</h2> <pre><code class="language-sql"><div class="example_code notranslate"> SELECT * FROM Products<br/>WHERE Price NOT BETWEEN 10 AND 20; </div></code></pre> </div> <h2>带有 IN 的 BETWEEN 操作符实例</h2> <p>下面的 SQL 语句选取价格介于 10 和 20 之间但 CategoryID 不为 1、2 或 3 的所有产品：</p> <div class="example margin-b-10"> <h2 class="example">实例</h2> <pre><code class="language-sql"><div class="example_code notranslate"> SELECT * FROM Products<br/>WHERE (Price BETWEEN 10 AND 20)<br/>AND NOT CategoryID IN (1,2,3); </div></code></pre> </div> <h2>带有文本值的 BETWEEN 操作符实例</h2> <p>下面的 SQL 语句选取 ProductName 以介于 'C' 和 'M' 之间字母开始的所有产品：</p> <div class="example margin-b-10"> <h2 class="example">实例</h2> <pre><code class="language-sql"><div class="example_code notranslate"> SELECT * FROM Products<br/>WHERE ProductName BETWEEN 'C' AND 'M'; </div></code></pre> </div> <h2>带有文本值的 NOT BETWEEN 操作符实例</h2> <p>下面的 SQL 语句选取 ProductName 不以介于 'C' 和 'M' 之间字母开始的所有产品：</p> <div class="example margin-b-10"> <h2 class="example">实例</h2> <pre><code class="language-sql"><div class="example_code notranslate"> SELECT * FROM Products<br/>WHERE ProductName NOT BETWEEN 'C' AND 'M'; </div></code></pre> </div> <h2>示例表</h2> <p>下面是选自 "Orders" 表的数据：</p> <table class="reference notranslate">
<tr>
<th>OrderID</th> <th>CustomerID</th> <th>EmployeeID</th> <th>OrderDate</th> <th>ShipperID</th> </tr>
<tr>
<td>10248</td> <td>90</td> <td>5</td> <td>7/4/1996</td> <td>3</td> </tr>
<tr>
<td>10249</td> <td>81</td> <td>6</td> <td>7/5/1996</td> <td>1</td> </tr>
<tr>
<td>10250</td> <td>34</td> <td>4</td> <td>7/8/1996</td> <td>2</td> </tr>
<tr>
<td>10251</td> <td>84</td> <td>3</td> <td>7/9/1996</td> <td>1</td> </tr>
<tr>
<td>10252</td> <td>76</td> <td>4</td> <td>7/10/1996</td> <td>2</td> </tr>
</table>
<h2>带有日期值的 BETWEEN 操作符实例</h2> <p>下面的 SQL 语句选取 OrderDate 介于 '04-July-1996' 和 '09-July-1996' 之间的所有订单：</p> <div class="example margin-b-10"> <h2 class="example">实例</h2> <pre><code class="language-sql"><div class="example_code notranslate"> SELECT * FROM Orders<br/>WHERE OrderDate BETWEEN #07/04/1996# AND #07/09/1996#; </div></code></pre> </div> <br/><table class="lamp"><tr>
<th width="34"><span aria-hidden="true" class="g-bg glyphicon glyphicon-flag margin-l-5"></span></th> <td> <p><b>请注意，在不同的数据库中，BETWEEN 操作符会产生不同的结果！<br/></b>在某些数据库中，BETWEEN 选取介于两个值之间但不包括两个测试值的字段。<br/>在某些数据库中，BETWEEN 选取介于两个值之间且包括两个测试值的字段。<br/>在某些数据库中，BETWEEN 选取介于两个值之间且包括第一个测试值但不包括最后一个测试值的字段。</p> <p><strong>因此，请检查您的数据库是如何处理 BETWEEN 操作符！</strong></p> </td> </tr></table>
<div class="text-center padding-10 margin-t-5">
</div>
</div>